﻿using H2.NET.Web.API.Common;
using H2.NET.Web.API.Model;
using H2.NET.Web.API.Service;
using H2.NET.Web.Pages.SMD.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace H2.NET.Web.API
{
    /// <summary>
    /// Summary description for CRMToSMD
    /// </summary>
    public class CRMToSMD : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpResult result = new HttpResult();
            try
            {
                HttpRequest Request = context.Request;
                Stream postData = Request.InputStream;
                StreamReader sRead = new StreamReader(postData, context.Request.ContentEncoding);
                string postContent = sRead.ReadToEnd();
                Logging.GetLogger().Error(postContent);
                sRead.Close();

                if (postContent != string.Empty && postContent != null)
                {
                    result = HandleData(postContent);
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().Error(ex.ToString());
                result.StatusCode = HttpStatusCode.ExpectationFailed;
                result.StatusDescription = ex.ToString();
            }


            //context.Response.Status = result.StatusCode.ToString();

            //context.Response.StatusDescription = result.StatusDescription;  

        }

        private HttpResult HandleData(string postData)
        {
            HttpResult result = new HttpResult();
            //string desKey = ConfigurationManager.AppSettings["DesKey"];
            //string jsString = DesMethod.MD5Decrypt(postData, desKey);
            SmdSync syncModel = new SmdSync();
            List<SmdSync> syncList = new List<SmdSync>();
            syncList = JsonHelper.DeserializeJsonToList<SmdSync>(postData);
            syncList = ProcessCRMData(syncList);

            if (DBService.SaveData(syncList))
            {
                result.StatusCode = HttpStatusCode.OK;
                result.StatusDescription = "数据保存成功。";
                result.Html = "<h1>数据保存成功</h1>";
            }
            else
            {
                result.StatusCode = HttpStatusCode.ExpectationFailed;
                result.StatusDescription = "数据保存失败。";
                result.Html = "<h1>数据保存失败</h1>";
            }

            return result;
        }


        public List<SmdSync> ProcessCRMData(List<SmdSync> smdSyncList)
        {
            List<SmdSync> result = new List<SmdSync>();
            foreach (SmdSync item in smdSyncList)
            {
                SmdSync temp = new SmdSync();
                temp = item;
                if (string.IsNullOrEmpty(item.StoreApprovedDate))
                {
                    temp.StoreApprovedDate = DateTime.Now.ToString("yyyy-MM-dd");
                }
                //switch (item.StoreType)
                //{
                //    case "DDCN":
                //        temp.StoreType = "多乐士漆体验中心";
                //        break;
                //    default:
                //        break;
                //}
                switch (item.Region)
                {
                    case "50001":
                    case "50006":
                        temp.AreaName = "South";
                        temp.AreaCode = item.Region;
                        break;
                    case "50002":
                    case "50007":
                        temp.AreaName = "East";
                        temp.AreaCode = item.Region;
                        break;
                    case "50003":
                    case "50005":
                        temp.AreaName = "North";
                        temp.AreaCode = item.Region;
                        break;
                    case "50004":
                    case "50008":
                        temp.AreaName = "West";
                        temp.AreaCode = item.Region;
                        break;
                    default:
                        temp.AreaName = item.Region;
                        temp.AreaCode = item.Region;
                        break;
                }
                switch (item.ShopSeq)
                {
                    case "1930001":
                        temp.ShopSeq = "1店";
                        break;
                    case "1930002":
                        temp.ShopSeq = "2店";
                        break;
                    case "1930003":
                        temp.ShopSeq = "3店";
                        break;
                    case "1930004":
                        temp.ShopSeq = "4店";
                        break;
                    case "1930005":
                        temp.ShopSeq = "5店";
                        break;
                    case "1930006":
                        temp.ShopSeq = "6店";
                        break;
                    case "1930007":
                        temp.ShopSeq = "7店";
                        break;
                    case "1930008":
                        temp.ShopSeq = "8店";
                        break;
                    case "1930009":
                        temp.ShopSeq = "9店";
                        break;
                    case "1930010":
                        temp.ShopSeq = "10店";
                        break;
                    default:
                        break;
                }
                switch (item.DeliveryType)
                {
                    case "1800001":
                        temp.DeliveryType = "新机安装(STD)";
                        break;
                    case "1800002":
                        temp.DeliveryType = "旧机转让(STD)";
                        break;
                    case "1800003":
                        temp.DeliveryType = "MT Automatic Machine Move(STD)";
                        break;
                    case "1800004":
                        temp.DeliveryType = "MT Manual Manchine Move(STD)";
                        break;
                    case "1800005":
                        temp.DeliveryType = "新机安装(DIR)";
                        break;
                    case "1800006":
                        temp.DeliveryType = "旧机转让(DIR)";
                        break;
                    case "1800007":
                        temp.DeliveryType = "无需机器";
                        break;
                    default:
                        break;
                }
                switch (item.ApplicationType)
                {
                    case "1800001":
                        temp.ApplicationType = "新机安装(STD)";
                        break;
                    case "1800002":
                        temp.ApplicationType = "旧机转让(STD)";
                        break;
                    case "1800003":
                        temp.ApplicationType = "MT Automatic Machine Move(STD)";
                        break;
                    case "1800004":
                        temp.ApplicationType = "MT Manual Manchine Move(STD)";
                        break;
                    case "1800005":
                        temp.ApplicationType = "新机安装(DIR)";
                        break;
                    case "1800006":
                        temp.ApplicationType = "旧机转让(DIR)";
                        break;
                    case "1800007":
                        temp.ApplicationType = "无需机器";
                        break;
                    default:
                        temp.ApplicationType = "无需机器";

                        break;
                }
                switch (item.IsRenew)
                {
                    case "1940001":
                        temp.IsRenew = "全新门店";
                        temp.IsUpgraded = "";
                        temp.IsDowngraded = "";
                        temp.NewFitmentName = "全新门店";
                        break;
                    case "1940002":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "从DCSS升级";
                        temp.IsDowngraded = "";
                        temp.NewFitmentName = "从DCSS升级";
                        break;
                    case "1940003":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "从DIS升级";
                        temp.IsDowngraded = "";
                        temp.NewFitmentName = "从DIS升级";
                        break;
                    case "1940004":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "";
                        temp.IsDowngraded = "从DDCN降级";
                        temp.NewFitmentName = "从DDCN降级";
                        break;
                    case "1940005":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "";
                        temp.IsDowngraded = "从DCSS降级";
                        temp.NewFitmentName = "从DCSS降级";
                        break;
                    case "1940006":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "";
                        temp.IsDowngraded = "DDCN关店";
                        temp.NewFitmentName = "DDCN关店";
                        break;
                    case "1940007":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "翻新";
                        temp.IsDowngraded = "";
                        temp.NewFitmentName = "翻新";
                        break;
                    case "1940008":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "仅调色机申请";
                        temp.IsDowngraded = "";
                        temp.NewFitmentName = "仅调色机申请";
                        break;
                    case "1940009":
                        temp.IsRenew = "";
                        temp.IsUpgraded = "";
                        temp.IsDowngraded = "DCSS关店";
                        temp.NewFitmentName = "DCSS关店";
                        break;
                    default:
                        break;
                }
                switch (item.status_id)
                {
                    case "1790001":
                        temp.Status = "新建";
                        break;
                    case "1790002":
                        temp.Status = "使用";
                        break;
                    case "1790003":
                        temp.Status = "经销商寄存";
                        break;
                    case "1790004":
                        temp.Status = "公司大仓";
                        break;
                    case "1790005":
                        temp.Status = "报废";
                        break;
                    case "1790006":
                        temp.Status = "旧机更换";
                        temp.IsTransferMachine = true;
                        break;
                    default:
                        break;
                }
                switch (item.DispenseModel)
                {
                    case "1810001":
                        temp.DispenseModel = "BT-16";
                        break;
                    case "1810002":
                        temp.DispenseModel = "D200";
                        break;
                    case "1810003":
                        temp.DispenseModel = "TM300 1.1";
                        break;
                    case "1810004":
                        temp.DispenseModel = "53PD";
                        break;
                    case "1810005":
                        temp.DispenseModel = "53XD";
                        break;
                    case "1810006":
                        temp.DispenseModel = "M3";
                        break;
                    case "1810007":
                        temp.DispenseModel = "TM300 1.0";
                        break;
                    case "1810008":
                        temp.DispenseModel = "TM280";
                        break;
                    case "1810009":
                        temp.DispenseModel = "不需要";
                        break;
                    case "1810021":
                        temp.DispenseModel = "KD-20B";
                        break;
                    case "1810022":
                        temp.DispenseModel = "Leonardo16";
                        break;
                    case "1810023":
                        temp.DispenseModel = "HA200";
                        break;
                    case "1810013":
                        temp.DispenseModel = "Banco";
                        break;
                    case "1810024":
                        temp.DispenseModel = "MS3";
                        break;
                    case "1810025":
                        temp.DispenseModel = "None";
                        break;
                    default:
                        break;
                }
                switch (item.ShakerModel)
                {
                    case "1820001":
                        temp.ShakerModel = "SHH-IV";
                        break;
                    case "1820002":
                        temp.ShakerModel = "SO-40A";
                        break;
                    case "1820003":
                        temp.ShakerModel = "Vibro";
                        break;
                    case "1820004":
                        temp.ShakerModel = "三华S5";
                        break;
                    case "1820005":
                        temp.ShakerModel = "不需要";
                        break;
                    case "1820017":
                        temp.ShakerModel = "FoShan Kedi";
                        break;
                    case "1820018":
                        temp.ShakerModel = "FAST Galileo";
                        break;
                    case "1820019":
                        temp.ShakerModel = "FAST Mixer";
                        break;
                    case "1820020":
                        temp.ShakerModel = "SanHua S5";
                        break;
                    case "1820021":
                        temp.ShakerModel = "SO-400";
                        break;
                    case "1820022":
                        temp.ShakerModel = "VCM ST";
                        break;
                    case "1820023":
                        temp.ShakerModel = "None";
                        break;
                    default:
                        break;
                }
                switch (item.PcModel)
                {
                    case "1830001":
                        temp.PcModel = "新购Dell";
                        break;
                    case "1830002":
                        temp.PcModel = "转让Dell";
                        break;
                    case "1830003":
                        temp.PcModel = "不需要";
                        break;
                    default:
                        break;
                }
                switch (item.CloseType)
                {
                    case "1950001":
                        temp.CloseType = "A.租金提高";
                        break;
                    case "1950002":
                        temp.CloseType = "B.运营原因";
                        break;
                    case "1950003":
                        temp.CloseType = "C.市场原因";
                        break;
                    case "1950004":
                        temp.CloseType = "D.渠道调整";
                        break;
                    case "1950005":
                        temp.CloseType = "E.店面到期";
                        break;
                    case "1950006":
                        temp.CloseType = "F.店面升级";
                        break;
                    case "1950007":
                        temp.CloseType = "G.店面迁址";
                        break;
                    default:
                        break;
                }
                temp.Address = temp.StoreAddress;
                temp.ApplicationTypeName = item.ApplicationType;
                temp.CertiCode = temp.AuthID;
                //temp.CreatorEmpDN = "d30.intra/wangc21";
                temp.CustomerCode = temp.DealerCode;
                temp.CustomerName = temp.CustomerShortName;
                temp.Shaker = temp.ShakerModel;
                temp.ShopTypeName = item.StoreType;
                temp.SubmitDate = DateTime.Now;
                temp.DisplayArea = Double.Parse(item.StoreSize);
                temp.ContactPerson = item.ContractPerson;
                temp.TintingTypeName = temp.DispenseModel;
                temp.StreetFullName = item.MallName;
                temp.StreetShortName = item.MallShortName;
                temp.ShopName = item.StoreName;
                temp.ShopNo = temp.ShopSeq;
                temp.AccountOwner = string.Empty;
                temp.SerialNumber = string.Format("{0}/{1}/{2}", item.EapproveSN1, item.EapproveSN2, DateTime.Now.ToString("yyyyMMddHHmmss"));
                if (temp.StoreAddress.Split(';').Count() > 0)
                {
                    if (temp.StoreAddress.Split(';')[0] == "上海" || temp.StoreAddress.Split(';')[0] == "重庆" || temp.StoreAddress.Split(';')[0] == "天津" || temp.StoreAddress.Split(';')[0] == "北京")
                    {
                        temp.StoreAddress = temp.StoreAddress.Remove(0, 3);
                    }
                }
                temp.StoreAddress = temp.StoreAddress.Replace(";", "");
                if (item.StoreAddress.Split(';').Count() > 0)
                {
                    temp.DoorNo = item.StoreAddress.Split(';')[item.StoreAddress.Split(';').Count() - 1];
                }
                //temp.Address = temp.StoreAddress;
                temp.Address = string.Format("{0}{1}", item.StoreAddress, item.HouseNumber);
                try
                {
                    if (temp.CloseDate != null && temp.CloseDate.Length == 8)
                    {
                        temp.CloseDwnDate = DateTime.ParseExact(temp.CloseDate, "yyyyMMdd", CultureInfo.CurrentCulture);
                    }
                }
                catch
                {
                    temp.CloseDwnDate = null;
                }
                result.Add(temp);
            }
            return result;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}